package cn.iocoder.yudao.module.wrzj.controller.app;

import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;

import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getRequest;

public class BaseWrzjController {

    private static final String KEY = "%4Yq0fRU2n@Bc$Yilv8DXBmH";

    /**
     * 生成登录用户的加密id
     *
     * @param key
     * @return
     */
    public String generateUserToken(String key) {
        return encrypt(key);
    }

    /**
     * 获取当前用户的解密id
     *
     * @return
     */
    public Long getLoginUid() {
        String token = getRequest().getHeader("X-Access-Token");
        return Long.valueOf(decryptStr(token));
    }


    /**
     * 会话id目前不存入数据库，默认算法为：用户id_伟人id，拼接字符串的加密值z
     *
     * @param greatmanId
     * @return
     */
    public String convertConversationId(String greatmanId) {
        String key = getLoginUid() + "_" + greatmanId;
        return encrypt(key);
    }

    public static String encrypt(String value) {
        byte[] key = KEY.getBytes();
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
        String encryptHex = aes.encryptHex(value);
        return encryptHex;
    }

    public static String decryptStr(String value) {
        byte[] key = KEY.getBytes();
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
        String encryptHex = aes.decryptStr(value);
        return encryptHex;
    }

    public static void main(String[] args) {
        System.out.println(new BaseWrzjController().decryptStr("05b534b94fc017569ab79c5020d1ea71"));
    }
}
